#include "main_time.hpp"
using namespace std;

void Quedr_Raw_Time_Debug(){
	se::Chrono c;
	se::Chrono c2;
	se::Chrono cFils(&c);
	char req;
	while(req != 'Q'){
		se::Global::time.Update();
		cout <<"Father 1 : "<< c.Time_s() << endl;
		cout <<"Father 2 : "<< c2.Time_s() << endl;
		cout <<"Son : "<< cFils.Time_s() << endl;

		req = getchar();

		se::Global::time.Update();
		if(req=='P'){
			if(!c.IsPaused()){
				c.Pause();
				cout<<"Paused father"<<endl;
			}
		}else if(req=='R'){
			if(c.IsPaused()){
				c.Resume();
				cout<<"Resumed father"<<endl;
			}
		}else if(req=='p'){
			if(!cFils.IsPaused()){
				cFils.Pause();
				cout<<"Resumed son"<<endl;
			}
		}else if(req=='r'){
			if(cFils.IsPaused()){
				cFils.Resume();
				cout<<"Resumed son"<<endl;
			}
		}else if(req=='c'){
				cFils.SetFather(&c2);
				cout<<"Changed father"<<endl;
		}
	}
}

string to_string(double x)
{
    string s;
    stringstream f;
    
    f << x;
    f >> s;
    
    return s;
}

bool Xyrion_time_debug(sf::RenderWindow &window)
{
    stringstream toString;
    const sf::Input &Input = window.GetInput();
    sf::Event Event;
        
    se::Chrono *c1 = new se::Chrono;
    string s1("chrono 1 : ");
    sf::String textC1(s1, sf::Font::GetDefaultFont(), 20);
    textC1.Move(100.f, 50.f);
    //cout<<"c1 cree"<<endl;
    
    se::Chrono *c2 = new se::Chrono;
    string s2("chrono 2 : ");
    sf::String textC2(s2, sf::Font::GetDefaultFont(), 20);
    textC2.Move(100.f, 150.f);
    //cout<<"c2 cree"<<endl;
    
    se::Chrono *c3 = new se::Chrono(c1);
    string s3("chrono 3 fils de 1 : ");
    sf::String textC3(s3, sf::Font::GetDefaultFont(), 20);
    textC3.Move(100.f, 250.f);
    //cout<<"c3 cree"<<endl;
    
    se::Chrono *c4 = new se::Chrono();
    c4->SetFather(c1);
    string s4("chrono 4 fils de 1 : ");
    sf::String textC4(s4, sf::Font::GetDefaultFont(), 20);
    textC4.Move(100.f, 350.f);
    //cout<<"c4 cree"<<endl;
    
    se::Chrono *c5 = new se::Chrono(c1);
    c5->SetFather(c2);
    string s5("chrono 5 fils de 2 : ");
    sf::String textC5(s5, sf::Font::GetDefaultFont(), 20);
    textC5.Move(100.f, 450.f);
    //cout<<"c5 cree"<<endl;
    
    se::Chrono *c6 = new se::Chrono(c3);
    string s6("chrono 6 fils de 3 : ");
    sf::String textC6(s6, sf::Font::GetDefaultFont(), 20);
    textC6.Move(100.f, 550.f);
    //cout<<"c6 cree"<<endl;
    
    se::Chrono *c7 = new se::Chrono(c1);
    c7->SetFather(NULL);
    string s7("chrono 7 : ");
    sf::String textC7(s7, sf::Font::GetDefaultFont(), 20);
    textC7.Move(100.f, 650.f);
    //cout<<"c7 cree"<<endl;
    
    bool run = true;
    
    while(run)
    {
            
        se::Global::time.Update();
        
        window.Clear();
        
        if(c1 != NULL)
        {
            if(c1->IsPaused()){textC1.SetText((s1+to_string(c1->Time_s())+" paused"));}
            else {textC1.SetText((s1+to_string(c1->Time_s())));}
        }
        else
        {
            textC1.SetText(s1+"deleted");
        }
        window.Draw(textC1);
        
        if(c2 != NULL)
        {
            if(c2->IsPaused()){textC2.SetText((s2+to_string(c2->Time_s())+" paused"));}
            else {textC2.SetText((s2+to_string(c2->Time_s())));}
        }
        else
        {
            textC2.SetText(s2+"deleted");
        }
        window.Draw(textC2);
        
        if(c3 != NULL)
        {
            if(c3->IsPaused()){textC3.SetText((s3+to_string(c3->Time_s())+" paused"));}
            else {textC3.SetText((s3+to_string(c3->Time_s())));}
        }
        else
        {
            textC3.SetText(s3+"deleted");
        }
        window.Draw(textC3);
        
        if(c4 != NULL)
        {
            if(c4->IsPaused()){textC4.SetText((s4+to_string(c4->Time_s())+" paused"));}
            else {textC4.SetText((s4+to_string(c4->Time_s())));}
        }
        else
        {
            textC4.SetText(s4+"deleted");
        }
        window.Draw(textC4);
        
        if(c5 != NULL)
        {
            if(c5->IsPaused()){textC5.SetText((s5+to_string(c5->Time_s())+" paused"));}
            else {textC5.SetText((s5+to_string(c5->Time_s())));}
        }
        else
        {
            textC5.SetText(s5+"deleted");
        }
        window.Draw(textC5);
        
        if(c6 != NULL)
        {
            if(c6->IsPaused()){textC6.SetText((s6+to_string(c6->Time_s())+" paused"));}
            else {textC6.SetText((s6+to_string(c6->Time_s())));}
        }
        else
        {
            textC6.SetText(s6+"deleted");
        }
        window.Draw(textC6);
        
        if(c7 != NULL)
        {
            if(c7->IsPaused()){textC7.SetText((s7+to_string(c7->Time_s())+" paused"));}
            else {textC7.SetText((s7+to_string(c7->Time_s())));}
        }
        else
        {
            textC7.SetText(s7+"deleted");
        }
        window.Draw(textC7);
        
        if(window.GetEvent(Event))
        {
            if(Event.Type == sf::Event::MouseButtonPressed)
            {
                if(Event.MouseButton.Button == sf::Mouse::Left)
                {
                    if(Input.GetMouseY() > 600)
                    {
                        c7->Pause();
                        cout<<"c7 paused"<<endl;
                    }
                    else if(Input.GetMouseY() > 500)
                    {
                        c6->Pause();
                        cout<<"c6 paused"<<endl;
                    }
                    else if(Input.GetMouseY() > 400)
                    {
                        c5->Pause();
                        cout<<"c5 paused"<<endl;
                    }
                    else if(Input.GetMouseY() > 300)
                    {
                        c4->Pause();
                        cout<<"c4 paused"<<endl;
                    }
                    else if(Input.GetMouseY() > 200)
                    {
                        c3->Pause();
                        cout<<"c3 paused"<<endl;
                    }
                    else if(Input.GetMouseY() > 100)
                    {
                        c2->Pause();
                        cout<<"c2 paused"<<endl;
                    }
                    else
                    {
                        c1->Pause();
                        cout<<"c1 paused"<<endl;
                    }
                }
                else if(Event.MouseButton.Button == sf::Mouse::Right)
                {
                    if(Input.GetMouseY() > 600)
                    {
                        c7->Resume();
                        cout<<"c7 resumed"<<endl;
                    }
                    else if(Input.GetMouseY() > 500)
                    {
                        c6->Resume();
                        cout<<"c6 resumed"<<endl;
                    }
                    else if(Input.GetMouseY() > 400)
                    {
                        c5->Resume();
                        cout<<"c5 resumed"<<endl;
                    }
                    else if(Input.GetMouseY() > 300)
                    {
                        c4->Resume();
                        cout<<"c4 resumed"<<endl;
                    }
                    else if(Input.GetMouseY() > 200)
                    {
                        c3->Resume();
                        cout<<"c3 resumed"<<endl;
                    }
                    else if(Input.GetMouseY() > 100)
                    {
                        c2->Resume();
                        cout<<"c2 resumed"<<endl;
                    }
                    else
                    {
                        c1->Resume();
                        cout<<"c1 resumed"<<endl;
                    }
                }
                else if(Event.MouseButton.Button == sf::Mouse::Middle)
                {
                    if(Input.GetMouseY() > 600)
                    {
                        cout<<"mid clic 7"<<endl;
                        if(c7 != NULL)
                        {
                            delete c7;
                            c7 = NULL;
                            cout<<"c7 deleted"<<endl;
                        }
                        else 
                        {
                            c7 = new se::Chrono(c1);
                            c7->SetFather(NULL);
                            cout<<"c7 resurected"<<endl;
                        }
                    }
                    else if(Input.GetMouseY() > 500)
                    {
                        cout<<"mid clic 6"<<endl;
                        if(c6 != NULL)
                        {
                            delete c6;
                            c6 = NULL;
                            cout<<"c6 deleted"<<endl;
                        }
                        else 
                        {
                            c6 = new se::Chrono(c3);
                            cout<<"c6 resurected"<<endl;
                        }
                    }
                    else if(Input.GetMouseY() > 400)
                    {
                        cout<<"mid clic 5"<<endl;
                        if(c5 != NULL)
                        {
                            delete c5;
                            c5 = NULL;
                            cout<<"c5 deleted"<<endl;
                        }
                        else 
                        {
                            c5 = new se::Chrono(c1);
                            c5->SetFather(c2);
                            cout<<"c5 resurected"<<endl;
                        }
                    }
                    else if(Input.GetMouseY() > 300)
                    {
                        cout<<"mid clic 4"<<endl;
                        if(c4 != NULL)
                        {
                            delete c4;
                            c4 = NULL;
                            cout<<"c4 deleted"<<endl;
                        }
                        else 
                        {
                            c4 = new se::Chrono();
                            c4->SetFather(c1);
                            cout<<"c4 resurected"<<endl;
                        }
                    }
                    else if(Input.GetMouseY() > 200)
                    {
                        cout<<"mid clic 3"<<endl;
                        if(c3 != NULL)
                        {
                            delete c3;
                            c3 = NULL;
                            cout<<"c3 deleted"<<endl;
                        }
                        else 
                        {
                            c3 = new se::Chrono(c1);
                            cout<<"c3 resurected"<<endl;
                        }
                    }
                    else if(Input.GetMouseY() > 100)
                    {
                        cout<<"mid clic 2"<<endl;
                        if(c2 != NULL)
                        {
                            delete c2;
                            c2 = NULL;
                            cout<<"c2 deleted"<<endl;
                        }
                        else 
                        {
                            c2 = new se::Chrono();
                            c2->SetFather(NULL);
                            cout<<"c2 resurected"<<endl;
                        }
                    }
                    else
                    {
                        cout<<"mid clic 1"<<endl;
                        if(c1 != NULL)
                        {
                            delete c1;
                            c1 = NULL;
                            cout<<"c1 deleted"<<endl;
                        }
                        else 
                        {
                            c1 = new se::Chrono();
                            cout<<"c1 resurected"<<endl;
                        }
                    }
                }
            }
            
            
            if(((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) || (Event.Type == sf::Event::Closed))
            {
                run = false;
            }
        }
    
        window.Display();
    }
    
    window.Close();
    
    return true;
}

int main_time(sf::RenderWindow &window)
{
	Quedr_Raw_Time_Debug();
	Xyrion_time_debug(window);
    return 0;
}
